<!-- ProductCard.vue -->
<template>
  <div class="product-card" @click="goToItemDetail">
    <div class="product-image-container">
      <img
        :src="product.imageUrl"
        :alt="product.name"
        class="product-image"
        @load="$emit('imageLoad')" />
    </div>
    <div class="product-info">
      <div class="product-info-row">
        <h3>{{ product.name }}</h3>
        <p class="price">¥{{ product.price }}</p>
      </div>
      <div class="product-info-row">
        <p>销量: {{ product.sales }}</p>
        <p>评论: {{ product.comments }}</p>
      </div>
      <div class="product-info-row">
        <p>销量: {{ product.sales }}</p>
        <p>评论: {{ product.comments }}</p>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
  import { defineProps } from 'vue'
  import { useRouter } from 'vue-router'

  const router = useRouter()

  const props = defineProps<{
    product: {
      id: number
      imageUrl: string
      name: string
      price: number
      sales: number
      comments: number
      isAd: boolean
    }
  }>()

  defineEmits(['imageLoad'])

  const goToItemDetail = () => {
  router.push({
    name: 'item',
    params: { id: props.product.id },
    query: {
      from: 'product_list'
    }
  })
}
</script>

<style scoped>
  .product-card {
    display: flex;
    flex-direction: column;
    width: 220px;
    overflow: hidden;
    position: relative;
    transition: transform 0.3s ease;
    z-index: 1;
    cursor: pointer; /* 添加指针样式 */
  }

  .product-card:hover {
    transform: translateY(-5px); /* 添加悬停效果 */
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); /* 悬停阴影 */
  }

  .product-image-container {
    flex: 0 0 300px; /* 图片区域固定高度为 200px */
    border-radius: 8px 8px 0 0; /* 圆角样式 */
    overflow: hidden; /* 防止图片溢出 */
    border: 1px solid #ddd; /* 图片容器添加边框 */
  }

  .product-image {
    width: 100%;
    height: 100%;
    object-fit: cover; /* 确保图片填满容器，多余部分裁剪 */
    cursor: pointer; /* 鼠标悬停时显示指针 */
  }

  .product-info {
    padding: 10px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    width: 100%;
  }

  .product-info-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }

  .product-info h3 {
    font-size: 16px;
    font-weight: bold;
    margin: 0;
  }

  .product-info p {
    margin: 0;
    color: #666;
    font-size: 14px;
  }

  .product-info p.price {
    color: red;
    font-weight: bold;
  }

  .product-info-row:last-child {
    margin-bottom: 0; /* 最后一行不需要底部间距 */
  }
</style>